package com.jaryle.quartz.serviceImpl;

import com.jaryle.quartz.bean.AbstractTask;
import com.jaryle.quartz.service.SchedulerJobService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import javax.annotation.Resource;

/**
 * 描述:
 * 定时任务执行类
 *
 * @author jaryle
 * @version V1.0
 * @create 2018-11-29
 */
@Component
public class SchedulerJobServiceImpl implements SchedulerJobService {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
//    @Autowired
//    private CronTriggerFactoryBean cronTriggerFactoryBean;

    @Override
    public void scheduleJob() throws SchedulerException {
        /*
         *  此处可以先通过任务名查询数据库，如果数据库中存在该任务，则按照ScheduleRefreshDatabase类中的方法，更新任务的配置以及触发器
         *  如果此时数据库中没有查询到该任务，则按照下面的步骤新建一个任务，并配置初始化的参数，并将配置存到数据库中
         */
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();

//        JobDetail jobDetail = JobBuilder.newJob(AbstractTask.class) .withIdentity("job1", "group1").build();
//        // 每5s执行一次
//        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
//        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1") .withSchedule(scheduleBuilder).build();
//        scheduler.scheduleJob(jobDetail,cronTrigger);
    }
}
